package com.wmapp.web;

import java.text.SimpleDateFormat;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wmapp.domain.MCUConfig;
import com.wmapp.domain.NetworkAddress;
import com.wmapp.domain.RouteItem;
import com.wmapp.domain.SMTP;
import com.wmapp.language.Lang;
import com.wmapp.service.SetupService;
import com.wmapp.utils.HttpUtils;

@Controller
@RequestMapping("/setup")
public class SetupController {
	private static Log log = LogFactory.getLog(SetupController.class);
	
	private SetupService setupService;
	
	private Lang lang;
	
	@Autowired
	public void setLang(Lang lang) {
		this.lang = lang;
	}
	
	@Autowired
	public void setSetupService(SetupService setupService) {
		this.setupService = setupService;
	}
	
	@RequestMapping(value="/network/address",method=RequestMethod.GET)
	public String showNetworkInfo(Model model){
		model.addAttribute("model", setupService.getNetworkAddress());
		return "networkAddress";
	}
	
	@RequestMapping(value="/network/address/json",method=RequestMethod.GET)
	@ResponseBody
	public String getNetAddressAsJSON(){
		try {
			return JSONObject.fromObject(setupService.getNetworkAddress()).toString();
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/network/address",method=RequestMethod.PUT)
	@ResponseBody
	public String saveNetworkAddress(NetworkAddress networkAddress){
		try {
			setupService.saveNetworkAddress(networkAddress);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/reboot",method=RequestMethod.GET)
	@ResponseBody
	public String rebootMCU(){
		try {
			setupService.rebootMCU();
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/routes/list",method=RequestMethod.GET)
	@ResponseBody
	public String listRoutes(){
		List<RouteItem> list = setupService.queryRoutes();
		
		return HttpUtils.domainList2GridRecords(list, RouteItem.class, RouteItem.GRID_COL_FIELDS, "net").toString();
	}
	
	@RequestMapping(value="/routes/route",method=RequestMethod.GET)
	public String enterRoute(){
		
		return "route";
	}
	
	@RequestMapping(value="/routes/route",method=RequestMethod.POST)
	@ResponseBody
	public String addRoute(RouteItem route){
		try {
			setupService.addRoute(route);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/routes",method=RequestMethod.DELETE)
	@ResponseBody
	public String remvoeRoutes(@RequestParam(value="routesJSON",required=true) String routesJSON){
		try {
			List<RouteItem> list=(List<RouteItem>)JSONArray.toCollection(JSONArray.fromObject(routesJSON),RouteItem.class);
			setupService.removeRoutes(list);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/mcu/config/enter",method=RequestMethod.GET)
	public String enterMCUConfig(Model model){
		model.addAttribute("model", setupService.getMCUConfig());
		return "mcuConfig";
	}
	
	@RequestMapping(value="/mcu/config",method=RequestMethod.PUT)
	@ResponseBody
	public String saveMCUConfig(MCUConfig mcuConfig){
		try {
			setupService.saveMCUConfig(mcuConfig);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/dates/enter",method=RequestMethod.GET)
	public String enterDates(){
		return "dates";
	}
	
	@RequestMapping(value="/dates",method=RequestMethod.PUT)
	@ResponseBody
	public String modifyDates(@RequestParam(value="dates",required=true) String dates){
		try {
			SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			setupService.modifyTime(format.parse(dates));
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	// ------------------ SMTP -------------------
	@RequestMapping(value="/smtp",method=RequestMethod.GET)
	public String enterSMTP(Model model){
		SMTP smtp = setupService.getSMTP();
		
		model.addAttribute("model", smtp);
		return "smtp";
	}
	
	@RequestMapping(value="/smtp",method=RequestMethod.PUT)
	@ResponseBody
	public String saveSMTP(SMTP smtp){
		try {
			setupService.saveSMTP(smtp);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
}
